home *** CD-ROM | disk | FTP | other *** search
- package netscape.applet;
-
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Vector;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- import netscape.misc.WeakLink;
- import netscape.security.AppletSecurity;
- import netscape.security.AppletSecurityException;
- import netscape.security.Principal;
- import netscape.security.PrivilegeManager;
- import netscape.security.Target;
-
- public class AppletClassLoader extends ClassLoader {
- URL codeBaseURL;
- URL archiveURL;
- MozillaAppletContext context;
- static boolean wantTiming;
- long loaderTime;
- String scriptOrigin;
- boolean isReusable;
- Principal codeBasePrincipal;
- Principal[] matchPrincipalAry;
- public static final String MANIFEST_FILENAME = "META-INF/manifest.mf";
- private String archiveFile;
- private boolean capturedArchive;
- private int nativeArchive;
- private int nativeZig;
- byte[] inputBuf;
- static Vector classloaders = new Vector(10);
- private static boolean capturingClasses;
- private static final boolean MARIMBA_ENABLED = true;
- private CastanetChannelInfo channelInfo;
- private Hashtable codebaseEnv;
-
- String grabArchiveFile() throws IOException {
- String var1 = this.archiveURL.getFile();
- if (!var1.endsWith(".jar") && !var1.endsWith(".zip")) {
- return null;
- } else {
- String var2;
- if (capturingClasses) {
- int var3 = var1.lastIndexOf(47);
- String var4 = var1.substring(var3 + 1);
- var2 = this.getCaptureDir() + "/" + var4;
- } else {
- var2 = this.getTempName();
- if (var2 == null) {
- return null;
- }
- }
-
- SecurityManager.enablePrivilege("UniversalFileAccess");
- SecurityManager.enablePrivilege("UniversalConnect");
- FileOutputStream var14 = new FileOutputStream(var2);
- long var15 = 0L;
- if (wantTiming) {
- var15 = System.currentTimeMillis();
- }
-
- InputStream var6 = null;
-
- try {
- URLConnection var9 = this.archiveURL.openConnection();
- var9.setAllowUserInteraction(false);
- var6 = var9.getInputStream();
- if (this.inputBuf == null) {
- this.inputBuf = new byte[4096];
- }
-
- Thread var11 = Thread.currentThread();
-
- int var10;
- while((var10 = var6.read(this.inputBuf)) >= 0 && !var11.isInterrupted()) {
- var14.write(this.inputBuf, 0, var10);
- }
-
- if (var11.isInterrupted()) {
- throw new IOException("interrupted: " + this);
- }
- } finally {
- if (var6 != null) {
- var6.close();
- }
-
- if (var14 != null) {
- var14.close();
- }
-
- }
-
- SecurityManager.revertPrivilege();
- if (wantTiming) {
- long var7 = System.currentTimeMillis();
- long var16 = var7 - var15;
- this.loaderTime += var16;
- System.err.println("# Archive download time: " + this.archiveURL + ": " + var16 + " (total = " + this.loaderTime + ")");
- }
-
- if (capturingClasses) {
- System.err.println("#\tCaptured archive " + var2);
- this.capturedArchive = true;
- } else if (this.context != null && MozillaAppletContext.debug >= 2) {
- System.err.println("# Loading classes from downloaded archive file: " + this.archiveURL);
- }
-
- return var2;
- }
- }
-
- native String getTempName();
-
- private native int openArchive0(String var1) throws IOException;
-
- private native byte[] loadFromArchive0(int var1, String var2) throws IOException;
-
- private native void closeArchive0(int var1) throws IOException;
-
- void openArchive() {
- try {
- this.archiveFile = this.grabArchiveFile();
- if (this.archiveFile == null) {
- return;
- }
-
- this.nativeArchive = this.openArchive0(this.archiveFile);
- if (this.nativeArchive != 0) {
- this.nativeZig = this.setupZignature();
- if (this.nativeZig != 0) {
- if (!this.loadManifest(this.nativeArchive, this.nativeZig, this.codeBaseURL.getFile()) || this.loadSignatures(this.nativeArchive, this.nativeZig, this.codeBaseURL.getFile()) <= 0) {
- this.nativeZig = 0;
- }
-
- return;
- }
- }
- } catch (IOException var2) {
- ((Throwable)var2).printStackTrace();
- System.err.println("# Unable to load archive " + this.archiveURL + ": " + var2);
- this.nativeZig = 0;
- }
-
- }
-
- byte[] loadFromArchive(String var1) throws IOException {
- return this.nativeArchive != 0 ? this.loadFromArchive0(this.nativeArchive, var1) : null;
- }
-
- void closeArchive() {
- if (this.nativeArchive != 0) {
- try {
- this.closeArchive0(this.nativeArchive);
- if (this.context != null && MozillaAppletContext.debug >= 5) {
- System.err.println("# Closed downloaded archive: " + this.archiveURL);
- }
- } catch (IOException var3) {
- if (this.context != null && MozillaAppletContext.debug >= 5) {
- System.err.println("# Failed to close downloaded archive: " + this.archiveURL);
- }
- }
-
- this.nativeArchive = 0;
- if (!this.capturedArchive) {
- File var1 = new File(this.archiveFile);
- SecurityManager.enablePrivilege("UniversalFileRead");
- SecurityManager.enablePrivilege("UniversalFileDelete");
- boolean var2 = var1.delete();
- SecurityManager.revertPrivilege();
- if (this.context != null && MozillaAppletContext.debug >= 5) {
- if (var2) {
- System.err.println("# Deleted downloaded archive: " + this.archiveFile);
- return;
- }
-
- System.err.println("# Failed to delete downloaded archive: " + this.archiveFile);
- }
- }
- }
-
- }
-
- static synchronized AppletClassLoader getClassLoader(MozillaAppletContext var0, URL var1, URL var2, String var3, boolean var4) {
- boolean var5 = false;
- AppletClassLoader var6 = null;
- SecurityManager.enablePrivilege("UniversalConnect");
-
- for(int var8 = 0; var8 < classloaders.size(); ++var8) {
- WeakLink var9 = (WeakLink)classloaders.elementAt(var8);
- AppletClassLoader var10 = (AppletClassLoader)var9.get();
- if (var10 == null) {
- var5 = true;
- } else {
- String var11 = var1.getFile();
- int var12 = var11.lastIndexOf(47);
- URL var7 = var1;
- if (var12 > 0 && var12 < var11.length() - 1) {
- try {
- var7 = new URL(var1, var11.substring(0, var12 + 1));
- } catch (MalformedURLException var13) {
- }
- }
-
- if (var7.equals(var10.codeBaseURL)) {
- label89: {
- boolean var10000;
- if (var2 == null) {
- if (var10.archiveURL == null) {
- break label89;
- }
-
- var10000 = false;
- } else {
- var10000 = var2.equals(var10.archiveURL);
- }
-
- if (!var10000) {
- continue;
- }
- }
-
- label81: {
- boolean var16;
- if (var3 == null) {
- if (var10.scriptOrigin == null) {
- break label81;
- }
-
- var16 = false;
- } else {
- var16 = var3.equals(var10.scriptOrigin);
- }
-
- if (!var16) {
- continue;
- }
- }
-
- if ((var3 == null || var0 == var10.context) && var10.isReusable) {
- if (!var4) {
- var6 = var10;
- if (MozillaAppletContext.debug >= 8) {
- System.err.println("# reusing class loader " + var10 + "\n" + "#\tcontext = " + var0 + "\n" + "#\tcodebase = " + var7 + "\n" + "#\tarchive = " + var2 + "\n" + "#\tscriptOrigin = " + var3);
- }
- break;
- }
-
- if (MozillaAppletContext.debug >= 8) {
- System.err.println("# preventing reuse of " + var10);
- }
-
- var10.isReusable = false;
- }
- }
- }
- }
-
- SecurityManager.revertPrivilege();
- if (var6 == null) {
- var6 = new AppletClassLoader(var0, var1, var2);
- var6.scriptOrigin = var3;
- classloaders.addElement(new WeakLink(var6));
- }
-
- if (var5) {
- for(int var14 = classloaders.size() - 1; var14 >= 0; --var14) {
- WeakLink var15 = (WeakLink)classloaders.elementAt(var14);
- if (var15.get() == null) {
- classloaders.removeElementAt(var14);
- }
-
- if (MozillaAppletContext.debug >= 8) {
- System.err.println("# classloader was removed");
- }
- }
- }
-
- return var6;
- }
-
- AppletClassLoader(MozillaAppletContext var1, URL var2, URL var3) {
- this.capturedArchive = false;
- this.scriptOrigin = null;
- this.isReusable = true;
- this.codeBasePrincipal = new Principal(var2);
- String var4 = var2.getFile();
- int var5 = var4.lastIndexOf(47);
- if (var5 > 0 && var5 < var4.length() - 1) {
- try {
- var2 = new URL(var2, var4.substring(0, var5 + 1));
- } catch (MalformedURLException var6) {
- }
- }
-
- this.codeBaseURL = var2;
- this.archiveURL = var3;
- this.context = var1;
- if (MozillaAppletContext.debug > 5) {
- System.err.println("# new class loader " + this + "\n" + "#\tcontext = " + var1 + "\n" + "#\tcodebase = " + var2 + "\n" + "#\tarchive = " + var3);
- }
-
- if (this.archiveURL != null) {
- this.openArchive();
- }
-
- }
-
- void close() {
- if (this.archiveURL != null) {
- this.closeArchive();
- }
-
- if (this.channelInfo != null) {
- this.channelInfo.closeZips();
- }
-
- }
-
- protected void finalize() {
- this.close();
- if (MozillaAppletContext.debug >= 8) {
- System.err.println("# finalized: " + this);
- }
-
- }
-
- protected AppletClassLoader(URL var1) {
- this((MozillaAppletContext)null, var1, (URL)null);
- }
-
- String getScriptOrigin() {
- return this.scriptOrigin;
- }
-
- public URL getCodeBase() {
- return this.codeBaseURL;
- }
-
- private Class loadClass(String var1, URL var2, String var3) throws IOException {
- SecurityManager.enablePrivilege("UniversalConnect");
- byte[] var4 = var2.getData();
- SecurityManager.revertPrivilege();
- if (capturingClasses) {
- this.captureClassFile(var1, var4);
- }
-
- return ((ClassLoader)this).defineClass(var1, var4, 0, var4.length, this.nativeZig, var3);
- }
-
- public Class loadClass(String var1) throws ClassNotFoundException {
- return this.loadClass(var1, true);
- }
-
- protected final Class loadClass(String var1, boolean var2) throws ClassNotFoundException {
- Class var3;
- if (wantTiming) {
- long var4 = System.currentTimeMillis();
- var3 = this.loadClass1(var1, var2);
- long var6 = System.currentTimeMillis();
- long var8 = var6 - var4;
- this.loaderTime += var8;
- System.err.println("# Class load time: " + var1 + ": " + var8 + " (total = " + this.loaderTime + ")");
- } else {
- var3 = this.loadClass1(var1, var2);
- }
-
- return var3;
- }
-
- private Class loadClass1(String var1, boolean var2) throws ClassNotFoundException {
- Class var3 = ((ClassLoader)this).findLoadedClass(var1);
- if (var3 == null) {
- SecurityManager var4 = System.getSecurityManager();
- if (var4 != null) {
- int var5 = var1.lastIndexOf(46);
- if (var5 >= 0) {
- var4.checkPackageAccess(var1.substring(0, var5));
- }
- }
-
- try {
- return ((ClassLoader)this).findSystemClass(var1);
- } catch (NoClassDefFoundError var8) {
- if (this.channelInfo == null) {
- throw var8;
- }
- } catch (ClassNotFoundException var9) {
- }
-
- var3 = this.findClass(var1);
- }
-
- if (var3 == null) {
- throw new ClassNotFoundException(var1);
- } else {
- if (this.nativeArchive != 0) {
- System.getSecurityManager().checkURLConnect(this.codeBaseURL, this.archiveURL);
- }
-
- if (var2) {
- try {
- ((ClassLoader)this).resolveClass(var3);
- } catch (NoClassDefFoundError var6) {
- if (this.channelInfo == null) {
- throw var6;
- }
-
- throw new ClassNotFoundException(var1);
- } catch (UnknownError var7) {
- if (this.channelInfo == null) {
- throw var7;
- }
-
- throw new ClassNotFoundException(var1);
- }
- }
-
- return var3;
- }
- }
-
- private synchronized Class findClass(String var1) throws ClassNotFoundException {
- boolean var2 = true;
- Class var3 = ((ClassLoader)this).findLoadedClass(var1);
- if (var3 != null) {
- return var3;
- } else {
- if (MozillaAppletContext.debug >= 2) {
- System.err.println("# Loading class " + var1);
- }
-
- SecurityManager var4 = System.getSecurityManager();
- if (var4 != null) {
- int var5 = var1.lastIndexOf(46);
- if (var5 >= 0) {
- var4.checkPackageDefinition(var1.substring(0, var5));
- }
- }
-
- String var14 = var1.replace('.', '/') + ".class";
- if (this.nativeArchive != 0) {
- try {
- byte[] var6 = this.loadFromArchive(var14);
- if (var6 != null) {
- var3 = ((ClassLoader)this).defineClass(var1, var6, 0, var6.length, this.nativeZig, var14);
- if (var3 != null && this.context != null && MozillaAppletContext.debug >= 2) {
- System.err.println("# Loaded " + var14 + " from " + this.archiveURL);
- }
-
- var2 = false;
- }
- } catch (IOException var12) {
- if (MozillaAppletContext.debug >= 2) {
- System.err.println("# Failed to load " + var14 + " from " + this.archiveURL);
- }
- }
- }
-
- if (var3 == null) {
- URL var15;
- try {
- var15 = new URL(this.codeBaseURL, var14);
- } catch (MalformedURLException var9) {
- throw new ClassNotFoundException(var1);
- }
-
- if (this.context != null && MozillaAppletContext.debug >= 5) {
- System.err.println("# Fetching " + var15);
- }
-
- try {
- var3 = this.loadClass(var1, var15, var14);
- var2 = false;
- } catch (IOException var10) {
- if (this.channelInfo != null) {
- try {
- var3 = this.marimbaLoad(var1);
- var2 = false;
- } catch (IOException var8) {
- }
- }
-
- if (var3 == null) {
- throw new ClassNotFoundException(var1);
- }
- } catch (SecurityException var11) {
- if (this.context != null && MozillaAppletContext.debug >= 5) {
- System.err.println("# Exception while loading " + var14 + " from downloaded archive " + var15.toString() + "\n# Exception: " + ((Throwable)var11).toString());
- }
-
- throw var11;
- }
- }
-
- if (!var1.equals(var3.getName())) {
- Class var16 = var3;
- Object var13 = null;
- throw new ClassFormatError(var1 + " != " + var16.getName());
- } else {
- if (!var2) {
- this.setPrincipalAry(var3, var14);
- }
-
- return var3;
- }
- }
- }
-
- static boolean getCaptureClassFiles() {
- return capturingClasses;
- }
-
- static void setCaptureClassFiles(boolean var0) {
- capturingClasses = var0;
- }
-
- String getCaptureDir() throws IOException {
- String var1 = this.codeBaseURL.toString();
- if (var1.startsWith("http://")) {
- var1 = var1.substring(7);
- }
-
- if (var1.endsWith("/")) {
- var1 = var1.substring(0, var1.length() - 1);
- }
-
- var1 = var1.replace('/', '_').replace(':', '_');
- SecurityManager.enablePrivilege("UniversalFileRead");
- SecurityManager.enablePrivilege("UniversalFileWrite");
- File var2 = new File(var1);
- if ((!var2.exists() || !var2.isDirectory()) && !var2.mkdir()) {
- throw new IOException(var2 + " is not a directory");
- } else {
- return var1;
- }
- }
-
- void captureClassFile(String var1, byte[] var2) {
- try {
- String var3 = this.getCaptureDir();
- SecurityManager.enablePrivilege("UniversalFileRead");
- SecurityManager.enablePrivilege("UniversalFileWrite");
-
- File var6;
- do {
- int var4 = var1.indexOf(46);
- if (var4 == -1) {
- File var9 = new File(var3 + "/" + var1 + ".class");
- FileOutputStream var10 = new FileOutputStream(var9);
- var10.write(var2, 0, var2.length);
- System.err.println("#\tCaptured class " + var9);
- var10.close();
- return;
- }
-
- String var5 = var1.substring(0, var4);
- var1 = var1.substring(var4 + 1);
- var3 = var3 + "/" + var5;
- var6 = new File(var3);
- } while(var6.exists() && var6.isDirectory() || var6.mkdir());
-
- throw new IOException("Can't create " + var6);
- } catch (SecurityException var7) {
- System.err.println("#\tCan't capture class: " + var7);
- } catch (IOException var8) {
- System.err.println("#\tCan't capture class: " + var8);
- }
- }
-
- byte[] getResource(URL var1) {
- byte[] var2 = null;
- String var3 = var1.getFile();
- String var4 = this.codeBaseURL.getFile();
- if (!var3.startsWith(var4)) {
- return null;
- } else {
- String var5 = var3.substring(var4.length());
-
- try {
- var2 = this.loadFromArchive(var5);
- if (var2 != null && this.context != null && MozillaAppletContext.debug >= 5) {
- System.err.println("# Loaded " + var5 + " from " + this.archiveURL);
- }
-
- if (var2 == null && this.context != null && MozillaAppletContext.debug >= 5) {
- System.err.println("# " + var5 + " in " + this.archiveURL + " not found.");
- }
- } catch (IOException var6) {
- if (MozillaAppletContext.debug >= 5) {
- System.err.println("# Failed to load " + var5 + " from " + this.archiveURL);
- }
- }
-
- return var2;
- }
- }
-
- public InputStream getResourceAsStream(String var1) {
- System.getSecurityManager().checkResourceAccess(var1);
- Object var2 = null;
-
- try {
- URL var5 = new URL(this.codeBaseURL, var1);
- byte[] var3 = this.getResource(var5);
- return var3 == null ? null : new ByteArrayInputStream(var3);
- } catch (MalformedURLException var4) {
- return null;
- }
- }
-
- private boolean setPrincipalAry(Class var1, String var2) {
- Object var3 = null;
- int var4 = 1;
- Object[] var5 = getCertificates(this.nativeZig, var1, var2);
- if (var5 != null) {
- var4 += var5.length;
- }
-
- Principal[] var12 = new Principal[var4];
-
- for(int var6 = var4 - 1; var6-- > 0; var12[var6] = new Principal(14, (byte[])var5[var6], var1)) {
- }
-
- var12[var4 - 1] = this.codeBasePrincipal;
-
- try {
- if (this.matchPrincipalAry != null) {
- PrivilegeManager var9 = AppletSecurity.getPrivilegeManager();
- if (var9 != null && var9.comparePrincipalArray(var12, this.matchPrincipalAry) == 1) {
- var12 = new Principal[]{this.codeBasePrincipal};
- throw new AppletSecurityException("mismatch of principals");
- }
- }
- } finally {
- this.setPrincipals(var1, var12);
- }
-
- return true;
- }
-
- public boolean checkMatchPrincipalAlways(int var1) {
- PrivilegeManager var5 = AppletSecurity.getPrivilegeManager();
- if (var5 == null) {
- return true;
- } else {
- this.matchPrincipalAry = var5.getClassPrincipalsFromStack(var1 + 1);
- Enumeration var2 = ((ClassLoader)this).getClassNames();
-
- while(var2.hasMoreElements()) {
- String var4 = (String)var2.nextElement();
- Class var3 = ((ClassLoader)this).findLoadedClass(var4);
- if (var3 != null && !var5.checkMatchPrincipal(var3, var1 + 1)) {
- return false;
- }
- }
-
- return true;
- }
- }
-
- private native int setupZignature();
-
- private native int destroyZignature(int var1);
-
- private native boolean loadManifest(int var1, int var2, String var3);
-
- private native int loadSignatures(int var1, int var2, String var3);
-
- private native boolean verifySignature(int var1, Class var2, String var3);
-
- private static native Object[] getCertificates(int var0, Class var1, String var2);
-
- private native void setPrincipals(Class var1, Principal[] var2);
-
- public AppletClassLoader(URL var1, CastanetChannelInfo var2) {
- this((MozillaAppletContext)null, var1, (URL)null);
- this.channelInfo = var2;
- }
-
- public Class classFromBytes(byte[] var1, String var2) {
- if (this.channelInfo == null) {
- return null;
- } else {
- Class var3 = ((ClassLoader)this).findLoadedClass(var2);
- if (var3 != null) {
- return var3;
- } else {
- try {
- var3 = ((ClassLoader)this).findSystemClass(var2);
- if (var3 != null) {
- return var3;
- }
- } catch (NoClassDefFoundError var8) {
- } catch (ClassNotFoundException var9) {
- }
-
- SecurityManager var4 = System.getSecurityManager();
- if (var4 != null) {
- int var5 = var2.lastIndexOf(46);
- if (var5 >= 0) {
- var4.checkPackageDefinition(var2.substring(0, var5));
- }
- }
-
- PrivilegeManager var12 = PrivilegeManager.getPrivilegeManager();
- if (var12 != null) {
- Target var6 = Target.findTarget("MarimbaInternalTarget");
- if (var6 == null) {
- throw new AppletSecurityException("classFromBytes call denied");
- }
-
- var12.checkPrivilegeEnabled(var6);
- }
-
- try {
- var3 = ((ClassLoader)this).defineClass(var2, var1, 0, var1.length);
- return var3;
- } catch (NoClassDefFoundError var7) {
- return null;
- }
- }
- }
- }
-
- private Class marimbaLoad(String var1) throws IOException, ClassNotFoundException {
- if (this.channelInfo == null) {
- return null;
- } else {
- Class var2 = null;
- int var3 = this.channelInfo.getPathlen();
- if (var3 != 0) {
- String var4 = var1.replace('.', '/') + ".class";
-
- for(int var5 = 0; var5 < var3; ++var5) {
- URL var6 = this.channelInfo.getURL(var5);
- if (var6 != null) {
- try {
- var2 = this.loadClass(var1, new URL(var6, var4), var4);
- if (var2 != null) {
- return var2;
- }
- } catch (NoClassDefFoundError var14) {
- } catch (MalformedURLException var15) {
- } catch (FileNotFoundException var16) {
- }
- }
-
- ZipFile var7 = this.channelInfo.getZip(var5);
- if (var7 != null) {
- ZipEntry var8 = var7.getEntry(var4);
- if (var8 != null) {
- int var9 = (int)var8.getSize();
- byte[] var10 = new byte[var9];
- InputStream var11 = var7.getInputStream(var8);
-
- for(int var12 = 0; var12 < var9; var12 += var11.read(var10, var12, var9 - var12)) {
- }
-
- var11.close();
-
- try {
- var2 = ((ClassLoader)this).defineClass(var1, var10, 0, var10.length);
- } catch (NoClassDefFoundError var13) {
- }
-
- if (var2 != null) {
- return var2;
- }
- }
- }
- }
- }
-
- return null;
- }
- }
-
- public boolean marimbaCheckRead(String var1, boolean var2) {
- return this.channelInfo != null ? this.channelInfo.checkRead(var1, var2) : false;
- }
-
- public boolean marimbaCheckWrite(String var1, boolean var2) {
- return this.channelInfo != null ? this.channelInfo.checkWrite(var1, var2) : false;
- }
-
- public String marimbaGetHost() {
- return this.channelInfo != null ? this.channelInfo.getHost() : null;
- }
-
- boolean marimbaCheckThreadGroup(ThreadGroup var1) {
- return this.channelInfo == null ? false : this.channelInfo.checkThreadGroup(var1);
- }
-
- void marimbaCheckTopLevelWindow(Object var1) {
- if (this.channelInfo != null) {
- this.channelInfo.checkTopLevelWindow(var1);
- }
- }
-
- Hashtable getCodebaseEnv() {
- if (this.codebaseEnv == null) {
- this.initCodebaseEnv();
- }
-
- return this.codebaseEnv;
- }
-
- synchronized void initCodebaseEnv() {
- if (this.codebaseEnv == null) {
- this.codebaseEnv = new Hashtable();
- }
-
- }
- }
-